/*
The MIT License

Copyright (c) 2010 Michael Webb

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
 */
//EL
(function() {
	var components = {};
	/**
	 * Interface defined for a component.
	 */
	var IComponent = {
		/**
		 * Initializes the component
		 * @function InitializeComponent
		 * @param {Object} manager The component manager
		 */
		InitializeComponent : function(manager) {
		},
		/**
		 * Sets the component cmpid
		 * @function SetComponentID
		 * @param {string} cmpid The component id
		 */
		SetComponentID : function(cmpid) {
			this.__ca_component__id = cmpid;
		},
		/**
		 * Gets the component id
		 * @function GetComponentID
		 * @return {string} The component id
		 */
		GetComponentID : function() {
			return this.__ca_component__id;
		}
	};
	var BaseComponent = IComponent;
	CA.Interfaces.Create("IComponent", [ "InitializeComponent",
			"SetComponentID", "GetComponentID" ]);
	/**
	 * Component Manager
	 * @class
	 * @static
	 */
	CA.Components = {
		/**
		 * Gets a component by ID
		 * @function Get
		 * @param {string} component_id
		 * @return {Object} The component
		 */
		Get : function(component_id) {
			if (typeof (components[component_id]) !== "undefined") {
				return components[component_id];
			}
			return null;
		},
		/**
		 * Adds to an object the methods to allow it to participate as a
		 * component
		 * @function Decorate
		 * @param {Object} cmp The object to be decorated with component functions
		 */
		Decorate : function(cmp) {
			for ( var attr in BaseComponent) {
				if (BaseComponent.hasOwnProperty(attr) && !cmp.hasOwnProperty(attr)) {
					cmp[attr] = BaseComponent[attr];
				}
			}
		},
		/**
		 * Registers a component with the framework
		 * @function Register
		 * @param {Object} cmp The component being registered
		 */
		Register : function(cmp) {
			CA.Interfaces.Ensure(cmp, "IComponent");
			var component_id = CA.Services.Get("IUIDProvider", "default")["GetUID"]();
			cmp["SetComponentID"](component_id);
			components[component_id] = cmp;
			if (CA.ComponentCommunications) {
				CA.ComponentCommunications["_setupComponent"](cmp);
			}
			cmp["InitializeComponent"](this);
		},
		/**
		 * Unregisters a component from the framework
		 * @function Unregister
		 * @param {Object} cmp The component being unregistered
		 */
		Unregister : function(cmp) {
			CA.Interfaces.Ensure(cmp, "IComponent");
			var component_id = cmp["GetComponentID"]();
			delete components[component_id];
			if (CA.ComponentCommunications) {
				CA.ComponentCommunications["UnregisterAll"](cmp);
			}
		}
	};
})();
